/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Group;
import db.dao.GroupDAO;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mikael
 */
public class CreateGroupBean {
    private String name;
    private String description;

    @PostConstruct
    void init(){}

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void listener(ActionEvent e){
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);

        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);

        Group newGroup = new Group();
        newGroup.setCreatorId(lb.getId());
        newGroup.setName(name);
        newGroup.setDescription(description);

        ViewChangeBean vcb =
                (ViewChangeBean)https.getAttribute(ViewChangeBean.MANGED_BEAN_NAME);

        int groupId = GroupDAO.create(newGroup);

        vcb.setParam(groupId + "");
        vcb.setToView(e.getComponent().getId());
    }
}
